/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "DomainShader.h"
#include <D3D11.h>
#include "Hogshead.Common.h"
#include "../Managers/DX11Manager.h"

namespace Hogshead
{
	namespace DX11Rendering
	{
		ContentCPP(Hogshead::DX11Rendering::DomainShader, DomainShader)

		DomainShader::DomainShader(void) : _ds(NULL)
		{
		}


		DomainShader::~DomainShader(void)
		{
			if(_ds)
			{
				_ds->Release();
				_ds = NULL;
			}
		}

		void DomainShader::finalize()
		{
			if(_ds)
			{
				_ds->Release();
				_ds = NULL;
			}
		}

		void DomainShader::explicitLoad()
		{
			error("You cannont load a Domain Shader in this manor, XML is required");
		}

		void DomainShader::readXML( const String& in_xml )
		{
			//parse it for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			if(root_tag.equals("Content"))
			{
				//get the standard stuff
				
				this->_key = String(root_element->Attribute("key"));
				this->_path = String(root_element->Attribute("path"));

				//used to get the absolute path to it 
				this->_path = ContentLibrary::getCompleteContentString( this->_path );

				//get the shader specific stuff
				HRESULT h_result = S_OK;
				ID3DBlob* ds_blob = NULL;

				//create the shader blob
				h_result = Shader::CompileShaderFromFile( _path, root_element->Attribute("entry"), root_element->Attribute("model"), &ds_blob );
				if( h_result != S_OK )
				{
					error( StringBuilder() << "The Shader file cannot be compiled.  Please run this executable from the directory that contains the FX file. FILE: " << _path );
					ds_blob->Release();
					return;
				}

				//create the actual shader from the blob
				h_result = DX11Manager::createDSFromBlob(ds_blob, &_ds);

				if( h_result != S_OK )
				{
					error( StringBuilder() << "The Domain Shader could not be created from the byte code, FILE: " << _path);
					ds_blob->Release();
					return;
				}

#ifdef _DEBUG
				//write a debug object name for PIX
				h_result = _ds->SetPrivateData( WKPDID_D3DDebugObjectName, _path.length(), _path );
				if( h_result != S_OK )
				{
					error(StringBuilder() << "Couldn't name the Domain Shader on the device, FILE: " << _path );
					ds_blob->Release();
					return;
				}
#endif
				//release the blob ref
				ds_blob->Release();
			}
		}
	}
}